import json


def template(title, data):
    papers = []

    for k, i in enumerate(data):
        aList = []
        for au in i['authors']:
            aList.append({
                "type": "textRun",
                "textRun": {
                    "text": au['name'],
                    "style": {
                        "link": {
                            "url": au['url']
                        }
                    }
                }
            })
            aList.append({
                "type": "textRun",
                "textRun": {
                    "text": ", ",
                    "style": {}
                }
            })

        cList = []
        for ca in i['category']:
            cList.append({
                "type": "textRun",
                "textRun": {
                    "text": ca,
                    "style": {},
                    "location": {
                        "zoneId": "0",
                        "startIndex": 199,
                        "endIndex": 231
                    }
                }
            })
        cList[0]['textRun']['style'] = {"bold": True}

        comment = []
        if 'comments' in i:
            comment = [{
                "type": "paragraph",
                "paragraph": {
                    "elements": [
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": i['comments'],
                                "style": {},
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 170,
                                    "endIndex": 188
                                }
                            }
                        }
                    ],
                    "location": {
                        "zoneId": "0",
                        "startIndex": 170,
                        "endIndex": 188
                    },
                    "lineId": "3CkqFR"
                }
            }]

        summary = []
        if 'summary' in i:
            summary = [{
                "type": "paragraph",
                "paragraph": {
                    "elements": [
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": i['summary'],
                                "style": {},
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 265,
                                    "endIndex": 1338
                                }
                            }
                        }
                    ],
                    "location": {
                        "zoneId": "0",
                        "startIndex": 265,
                        "endIndex": 1338
                    },
                    "lineId": "X4c2Zt"
                }
            }]

        papers.extend([{
            "type": "paragraph",
            "paragraph": {
                "elements": [
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": " ",
                            "style": {},
                            "location": {
                                "zoneId": "0",
                                "startIndex": 17,
                                "endIndex": 18
                            }
                        }
                    },
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": i['title'],
                            "style": {
                                "bold": True
                            },
                            "location": {
                                "zoneId": "0",
                                "startIndex": 18,
                                "endIndex": 74
                            }
                        }
                    },
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": " ",
                            "style": {
                                "bold": True
                            },
                            "location": {
                                "zoneId": "0",
                                "startIndex": 74,
                                "endIndex": 75
                            }
                        }
                    }
                ],
                "style": {
                    "headingLevel": 6,
                    "list": {
                        "type": "number",
                        "indentLevel": 1,
                        "number": (k+1)
                    },
                    "collapse": True
                },
                "location": {
                    "zoneId": "0",
                    "startIndex": 16,
                    "endIndex": 75
                },
                "lineId": "D8qBU2"
            }
        },
        {
            "type": "paragraph",
            "paragraph": {
                "elements": aList,
                "location": {
                    "zoneId": "0",
                    "startIndex": 76,
                    "endIndex": 139
                },
                "lineId": "epBG4u"
            }
        },
        {
            "type": "paragraph",
            "paragraph": {
                "elements": [
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": f"arXiv:{i['rawId']}",
                            "style": {
                                "bold": True,
                                "link": {
                                    "url": f"https://arxiv.org/abs/{i['rawId']}"
                                }
                            },
                            "location": {
                                "zoneId": "0",
                                "startIndex": 140,
                                "endIndex": 156
                            }
                        }
                    },
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": " [",
                            "style": {},
                            "location": {
                                "zoneId": "0",
                                "startIndex": 156,
                                "endIndex": 158
                            }
                        }
                    },
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": "pdf",
                            "style": {
                                "bold": True,
                                "link": {
                                    "url": f"https://arxiv.org/pdf/{i['rawId']}"
                                }
                            },
                            "location": {
                                "zoneId": "0",
                                "startIndex": 158,
                                "endIndex": 161
                            }
                        }
                    },
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": ", ",
                            "style": {},
                            "location": {
                                "zoneId": "0",
                                "startIndex": 161,
                                "endIndex": 163
                            }
                        }
                    },
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": "other",
                            "style": {
                                "bold": True,
                                "link": {
                                    "url": f"https://arxiv.org/format/{i['rawId']}"
                                }
                            },
                            "location": {
                                "zoneId": "0",
                                "startIndex": 163,
                                "endIndex": 168
                            }
                        }
                    },
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": "]",
                            "style": {},
                            "location": {
                                "zoneId": "0",
                                "startIndex": 168,
                                "endIndex": 169
                            }
                        }
                    }
                ],
                "location": {
                    "zoneId": "0",
                    "startIndex": 140,
                    "endIndex": 169
                },
                "lineId": "PWwIT0"
            }
        },
        *comment,
        {
            "type": "paragraph",
            "paragraph": {
                "elements": [
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": "Subjects: ",
                            "style": {},
                            "location": {
                                "zoneId": "0",
                                "startIndex": 189,
                                "endIndex": 199
                            }
                        }
                    },
                    *cList
                ],
                "location": {
                    "zoneId": "0",
                    "startIndex": 189,
                    "endIndex": 264
                },
                "lineId": "fYSbdd"
            }
        },
        *summary,
        {
            "type": "paragraph",
            "paragraph": {
                "elements": [],
                "location": {
                    "zoneId": "0",
                    "startIndex": 1339,
                    "endIndex": 1339
                },
                "lineId": "FhnEVp"
            }
        }])
    
    return {
        "FolderToken": "",
        "Content": json.dumps({
            "title": {
                "elements": [
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": title,
                            "style": {},
                            "location": {
                                "zoneId": "0",
                                "startIndex": 0,
                                "endIndex": 8
                            }
                        }
                    }
                ],
                "location": {
                    "zoneId": "0",
                    "startIndex": 0,
                    "endIndex": 8
                },
                "lineId": "XxEzN0"
            },
            "body": {
                "blocks": papers
            }
        })
    }

